package com.sino.androiddesign.recyclerviewdemo


import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import com.sino.androiddesign.R
import com.sino.androiddesign.common.ItemClickListener
import kotlinx.android.synthetic.main.fragment_universal_adapter.*


/**
 * 万能适配器
 */
class UniversalAdapterFragment : Fragment() {

    private var datas: IntArray = intArrayOf(R.mipmap.ic01, R.mipmap.ic02, R.mipmap.ic03, R.mipmap.ic04, R.mipmap.ic05, R.mipmap.ic06)

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_universal_adapter, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        universal_rv.layoutManager = LinearLayoutManager(context!!)

        var arrayList = ArrayList<Bean>()
        for (i in 0 until 40) {
            arrayList.add(Bean("文字描述: $i", datas[i % datas.size]))
        }
        universal_rv.adapter = object : BaseRecyclerAdapter<Bean>(context!!, arrayList, R.layout.fragment_universal_adapter_item) {
            override fun convert(holder: BaseRecyclerHolder, item: Bean, position: Int) {
                holder.setImage(R.id.universal_item_iv, item.img)
                holder.setText(R.id.universal_item_tv, item.text)
            }
        }

    }
}

class Bean(var text: String, var img: Int)

/**
 * 基础的 Holder
 */
class BaseRecyclerHolder(context: Context, itemView: View?) : RecyclerView.ViewHolder(itemView) {

    // 指定初始为8
    private var views: SparseArray<View> = SparseArray(8)

    companion object {
        //获取一个holder 对象
        fun getRecyclerHolder(context: Context, itemView: View): BaseRecyclerHolder {
            return BaseRecyclerHolder(context, itemView)
        }
    }

    fun getViews(): SparseArray<View> {
        return this.views
    }

    //通过view的id 获取对应的控件,如果没有则加入views中
    private fun <T : View> getView(viewId: Int): T {
        var view = views.get(viewId)
        if (view == null) {
            view = itemView.findViewById(viewId)
            views.put(viewId, view)
        }
        return view as T
    }

    fun setText(viewId: Int, text: String): BaseRecyclerHolder {
        var textView = getView<TextView>(viewId)
        textView.text = text
        return this
    }

    fun setImage(viewId: Int, img: Int): BaseRecyclerHolder {
        var imageView = getView<ImageView>(viewId)
        imageView.setImageResource(img)
        return this
    }

}

/**
 * 万能适配器
 */
abstract class BaseRecyclerAdapter<T>(var context: Context, var datas: ArrayList<T>, var itemLayoutId: Int) : RecyclerView.Adapter<BaseRecyclerHolder>() {

    private var recyclerView: RecyclerView? = null

    private var inflater: LayoutInflater = LayoutInflater.from(context)

    private  var itemClickListener: ItemClickListener?=null

    fun setClickListener(clickListener: ItemClickListener) {
        this.itemClickListener = clickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseRecyclerHolder {

        var view = inflater.inflate(itemLayoutId, parent, false)
        return BaseRecyclerHolder.getRecyclerHolder(context, view)
    }

    override fun getItemCount(): Int {

        return datas.size
    }

    override fun onBindViewHolder(holder: BaseRecyclerHolder, position: Int) {
        convert(holder, datas[position], position)
        if (itemClickListener != null) {
            holder.itemView.setOnClickListener {
                itemClickListener!!.clickItemListener(position)
            }
        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        this.recyclerView = recyclerView
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        this.recyclerView = null
    }

    abstract fun convert(holder: BaseRecyclerHolder, item: T, position: Int)

    fun delete(position: Int) {
        datas.removeAt(position)
        notifyItemRemoved(position)
    }

    fun insert(item: T, position: Int) {
        datas.add(position, item)
        notifyItemInserted(position)
    }
}